package com.fare.taluo.application.impl.queue;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.domain.Entity;
import com.dayatang.querychannel.support.Page;
import com.fare.taluo.application.queue.QueueEmailMessageApplication;
import com.fare.taluo.core.domain.queue.QueueBaseEntity;
import com.fare.taluo.core.domain.queue.message.QueueEmailMessageEntity;
import com.fare.taluo.core.domain.system.EmailEntity;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.vo.queue.QueueEmailMessageVO;
import com.fare.taluo.vo.queue.QueueEmailMessageVOListByDate;
/**
 * 邮件发送应用层
 * @author creator: xiangsf
 * @author modifior: xiangsf
 * @since 2012-12-19 下午1:51:22
 * @version v1.0
 */
@Transactional
@Scope("singleton")
@Service("queueEmailMessageApplication")
public class QueueEmailMessageApplicationImpl extends ScheduleQueueApplicationImpl<QueueEmailMessageVO> 
	implements QueueEmailMessageApplication{

	@Override
	protected QueueBaseEntity<? extends Entity> newInstance() throws Exception {
		return new QueueEmailMessageEntity();
	}

//	@Override
//	public boolean archive(QueueEmailMessageVO vo) throws Exception {
//		// TODO Auto-generated method stub
//		return false;
//	}

	@Override
	public List<QueueEmailMessageVO> findEntityByPage(
			QueueEmailMessageEntity query, int pageNo, int pageSize) {
		
//		List<QueueEmailMessageVO> listVo=new ArrayList<QueueEmailMessageVO>();
		EmailEntity en =new EmailEntity();
		Page<QueueEmailMessageEntity> pageEntity= en.findEntityByPage(query, pageNo, pageSize);
		List<QueueEmailMessageEntity> listEntity=pageEntity.getResult();
		List<QueueEmailMessageVO> listVo=explanationQueueEmailMessageEntity(listEntity);
		/*if(listEntity.size()>0){
			for(QueueEmailMessageEntity entity:listEntity){
				QueueEmailMessageVO vo =new QueueEmailMessageVO();
				vo.setAttachPath(entity.getAttachPath());
				vo.setBody(entity.getBody());
				vo.setCreateTime(entity.getCreateTime());
				vo.setCreateTimeStr(DateUtils.dateToStr(entity.getCreateTime(),"yyyy-MM-dd"));
				vo.setEndTime(entity.getEndTime());
				vo.setId(entity.getId());
				vo.setName(entity.getName());
				vo.setPlanStartTime(entity.getPlanStartTime());
				vo.setToAddress(entity.getToAddress());
				vo.setSubject(entity.getSubject());
				vo.setStatus(entity.getStatus());
				vo.setStartTime(entity.getStartTime());
				vo.setSpentTimes(entity.getSpentTimes());
				
				listVo.add(vo);
			}
		}*/
		
		return listVo;
	}

	
	public static List<QueueEmailMessageVO> explanationQueueEmailMessageEntity(List<QueueEmailMessageEntity> listEntity){
		List<QueueEmailMessageVO> listVo=new ArrayList<QueueEmailMessageVO>();
		if(listEntity.size()>0){
			for(QueueEmailMessageEntity entity:listEntity){
				QueueEmailMessageVO vo =new QueueEmailMessageVO();
				if(entity.getAttachPath() !=null && entity.getSubject() !=null && entity.getCreateTime()!=null){
					String paths=entity.getAttachPath().replaceAll("\\\\","/").replaceAll("\\\\\\\\", "/");
					vo.setAttachPath(paths);
					vo.setBody(entity.getBody());
					vo.setCreateTime(entity.getCreateTime());
					vo.setCreateTimeStr(DateUtils.dateToStr(entity.getCreateTime(),"yyyy-MM-dd"));
					vo.setEndTime(entity.getEndTime());
					vo.setId(entity.getId());
					vo.setName(entity.getName());
					vo.setPlanStartTime(entity.getPlanStartTime());
					vo.setToAddress(entity.getToAddress());
					vo.setSubject(entity.getSubject());
					vo.setStatus(entity.getStatus());
					vo.setStartTime(entity.getStartTime());
					vo.setSpentTimes(entity.getSpentTimes());
					vo.setStatusStr(entity.getStatus()+"");
					listVo.add(vo);
				}
			}
		}
		return listVo;
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<QueueEmailMessageVOListByDate> findListByList(
			List<QueueEmailMessageVO> list) {
		Map<String ,List<QueueEmailMessageVO>> mapList=new HashMap<String, List<QueueEmailMessageVO>>();
		if(list.size()>0){
			for(int i=0;i<list.size();i++){
				QueueEmailMessageVO old=list.get(i);
				String key=old.getCreateTimeStr();
				if(mapList.get(key)!=null){//表示已经存在有了
					mapList.get(key).add(old);
					
				}else{//存在没有，得新建一个key
					List<QueueEmailMessageVO> newVo=new ArrayList<QueueEmailMessageVO>();
					newVo.add(old);
					mapList.put(key, newVo);
				}
			}
		}
		
		Map<String ,List<QueueEmailMessageVO>> mapNameList=new HashMap<String, List<QueueEmailMessageVO>>();
		//再次过滤重复的文件名
		if(mapList.size()>0){
			for(Map.Entry<String,List<QueueEmailMessageVO>> mapLists:mapList.entrySet()){
				String keys=mapLists.getKey();
				List<QueueEmailMessageVO> lists=mapLists.getValue();
				Map<String, QueueEmailMessageVO > mapNames=new HashMap<String, QueueEmailMessageVO>();
				for(int m=0;m<lists.size();m++){
					QueueEmailMessageVO vo=lists.get(m);
					String key=vo.getAttachPath();
					if(mapNames.get(key) !=null){
						QueueEmailMessageVO newVo=new QueueEmailMessageVO();
						newVo.setAttachPath(vo.getAttachPath());
						if(DateUtils.compareDateByDate(vo.getCreateTime(), mapNames.get(key).getCreateTime())){
							newVo.setCreateTime(vo.getCreateTime());
							newVo.setAttachPath(vo.getAttachPath());
						}	
						
					}else{
						mapNames.put(key, vo);
					}
				}
				List<QueueEmailMessageVO> listNameVo=new ArrayList<QueueEmailMessageVO>();
				Iterator nameMaps=mapNames.entrySet().iterator();
				while(nameMaps.hasNext()){
					Entry nameVo=(Entry)nameMaps.next();
					listNameVo.add((QueueEmailMessageVO) nameVo.getValue());
				}
				
				mapNameList.put(keys, listNameVo);
			}
		}
		
		List<QueueEmailMessageVOListByDate> voList = new ArrayList<QueueEmailMessageVOListByDate>();
		if(mapNameList.size()>0){
			Iterator iter = mapNameList.entrySet().iterator();  //获得map的Iterator
			while(iter.hasNext()) {
				Entry entry = (Entry)iter.next();
				
				QueueEmailMessageVOListByDate bigVo=new QueueEmailMessageVOListByDate();
				bigVo.setListQueEMvo((List<QueueEmailMessageVO>) entry.getValue());
				bigVo.setCreateTimeStr(entry.getKey().toString());
				voList.add(bigVo);
			}
		}
		
		//按生成日期排序——降序
		sortClass sort = new sortClass();
		Collections.sort(voList,sort);	
			
			
		return voList;
	}

		
	@Override
	public List<QueueEmailMessageVO> findEntityByDate(Date createDate) {

		EmailEntity en =new EmailEntity();
		List<QueueEmailMessageEntity> listEntity= en.findEntityByDate(createDate);
		List<QueueEmailMessageVO> listVo=explanationQueueEmailMessageEntity(listEntity);
		
		
		return listVo;
	}
	
	public class sortClass implements Comparator {
		public int compare(Object arg0, Object arg1) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); 
			QueueEmailMessageVOListByDate user0 = (QueueEmailMessageVOListByDate) arg0;
			QueueEmailMessageVOListByDate user1 = (QueueEmailMessageVOListByDate) arg1;
			Date d0=null;
			Date d1=null;
			if(user0.getCreateTimeStr() !=null){
				try {
					d0=sdf.parse(user0.getCreateTimeStr());
					d1=sdf.parse(user1.getCreateTimeStr());
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
			 if(d1 ==null && d0==null){
				 return 0;
			 }
			if(d1 ==null ){
				return -1;
			}
			if(d0 ==null ){
				return 1;
			}
			return d1.compareTo(d0);
		}
	}

}


